﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

// 护盾
public class BuffProtect : BuffBase
{
    GameObject _effect;
    public override void Init(int id, HumanCtrl owner, Action<int> endCallback)
    {
        base.Init(id, owner, endCallback);

        Start();
    }

    protected override void onStart()
    {
        base.onStart();
        _effect = addEffect("Effect/Shield");
        checkWhenStart();
    }

    private void checkWhenStart()
    {
        var allCollider = Physics2D.OverlapCircleAll(_owner.transform.position, 0.56f, 1 << LayerMask.NameToLayer("Block") | 1 << LayerMask.NameToLayer("Enemy"));
        foreach (var collider in allCollider)
        {
            var enemy = collider.GetComponent<Enemy>();
            if (enemy == null) { OnBlockEnter(collider.transform); }
            else { OnEnemyEnter(enemy); }
        }
    }

    protected override void onFinish()
    {
        base.onFinish();
        GameObject.Destroy(_effect);
    }

    public override bool OnBlockEnter(Transform other)
    {
        GameObject.Destroy(other.gameObject);

        return true;
    }
    public override bool OnEnemyEnter(Enemy other)
    {
        other.BeAttacked(_owner);

        return true;
    }
}
